package org.springblade.modules.mes.opcua.itf.mock;

import lombok.extern.slf4j.Slf4j;
import org.springblade.modules.mes.opcua.itf.OpcUaItf;
import org.springblade.modules.mes.opcua.listener.KeyChangeListener;
import org.springblade.modules.mes.opcua.vo.OpcUaVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component(value = "mockOpcUa")
public class MockOpcUa implements OpcUaItf {

	private Map<String, Object> dataMap = new HashMap<>();

	@Resource
	private KeyChangeListener listener;

	@Override
	public void init() {
	}

	@Override
	public void writeValueShortFinal(OpcUaVO ctx, String position, String identifier, Object value) throws Exception {
		dataMap.put(ctx.opcuaKey(identifier, position), value);
	}


	@Override
	public void writeValue(OpcUaVO ctx, String position, String identifier, Object value) throws Exception {
		dataMap.put(ctx.opcuaKey(identifier, position), value);
	}

	@Override
	public Object readValue(String identifier) {
		return dataMap.get(identifier);
	}

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void fireEvent(String key, Object v) {
		listener.onDataValueReceived(key, v);
	}

}
